

function trim(val)
{
	 return val.replace(/(^\s*)|(\s*$)/g, "");  
}
 
function isMaxLen(o)
{  
	 var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";  
	 if(o.getAttribute && o.value.length>nMaxLen)
	 {      
	 	  o.value=o.value.substring(0,nMaxLen);  
	 }  
}
 
 
  function checkstate(pkid,bpm_id,sel){
    
	 if(window.confirm('确定要进行此操作码？')){
	  window.location.href='/doTradeReg.do?order_no='+pkid+'&bpm_id='+bpm_id+'&order_state='+sel;
 	}
}

  
function showAjaxContent(val)
{
   
   var data = Math.round(Math.random()* 10000);   
   var title ="";
   var gURL="";   
   if(val=='attr')
   {
     title="修改商品属性";     
     var goods_id = document.getElementById("goods_id").value;     
     gURL = "/program/admin/myorder/ajaxShowCatAtrr.jsp?height=300&width=500&goods_id="+goods_id+"&data="+data;
   } 
   if(val=='pack')
   {
      title="修改包装方式";     
      gURL = "/program/admin/myorder/ajaxShowPack.jsp?height=200&width=400&data="+data;
   }
   if(val=='invo')
   {
      title="修改发票信息";     
      gURL = "/program/admin/myorder/ajaxShowInvoice.jsp?height=200&width=400&data="+data;
   } 
   
   TB_show(title,gURL,false);    

}

function setGoodsAttr()
{
  //------  goods attr validate
	var attrSize = document.getElementById('attrsize').value;
	var attr_value='';
	for( var j = 0; j < attrSize; j++ )
	{ 
		var item;
		var attrStr ='';
		var attrId = document.getElementById('attr_id'+j).value;
		var attrName = document.getElementById('attr_name'+j).value;
		var conType = document.getElementById('con_type' + j).value;
		var defaultTag = document.getElementById('default_tag' + j).value;
		
		// select
		if(conType == '0')
		{ 
		  attrStr= document.getElementById('attr_value'+j).value;
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！'  );
 
				return;
			}  
		}
	 
	  // input
		if(conType == '1')
		{
				attrStr = document.getElementById('attr_value'+j).value; 
				if( attrStr == '' && defaultTag=='0')
				{
					alert('请输入' + attrName+'！');
 
					//document.getElementById('attr_id'+j).focus();
					return;
				}  
		}
		
		//radio
		if( conType == '2')
		{
			var valueSize = document.getElementById('valuesize'+j).value;
			for( var r = 0; r < valueSize; r++ ){
				 if( document.getElementById('attr_value'+j+'_'+r).checked){
					attrStr =  document.getElementById('attr_value'+j+'_'+r).value; 
				 } 
			}  
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！' );
				return;
			}  
		}
		if(attrStr != ''){
			item = attrId+"|"+attrStr;
			attr_value += item +',';
		}
	}
  var rdata = Math.round(Math.random()* 10000);
  var udata ='attr_value='+attr_value+'&rdata='+rdata; 
  jQuery.ajax({
		   type: 'POST',
		   url: '/program/admin/myorder/ajaxCatAttr.jsp',
		   data: udata,
       async:false,									   
		   success: function(msg){
         document.getElementById('g_attr_id').innerHTML = msg  
		   }
   }); 
 
   document.getElementById('goods_attr').value = attr_value;
   
   TB_remove();
           
}

 
function ajaxPackshow()
{
   var sel =	document.getElementById('packages');
 	 
	 if(sel.value == '')
   {
       alert("请选择包装方式"); 
   }
   else
   {
        //alert(sel.value)pack_id;
         document.getElementById('pack_id').value= sel.value;       
         document.getElementById('g_pack').innerHTML = sel.options[sel.selectedIndex].text;  
         TB_remove();
   }   
}


function ajaxInvoiceshow()
{
    var invoice_top =	document.getElementById('a_invoice_top');
    var invoice_no =	document.getElementById('a_invoice_no');
    var invoice_content =	document.getElementById('a_invoice_content');
    if(invoice_no.value=='')
    {
        alert("请选择发票类型！");
        return;    
    }
    if(invoice_top.value=='')
    {
        alert("请选择发票抬头！");
        return;    
    }
    if(invoice_content.value=='')
    {
        alert("请选择发票内容！");
        return;    
    } 
    var no_str = invoice_no.options[invoice_no.selectedIndex].text; 
    var top_str = invoice_top.options[invoice_top.selectedIndex].text; 
    var content_str = invoice_content.options[invoice_content.selectedIndex].text;
    document.getElementById('g_invoice').innerHTML = '发票类型:'+no_str+' 发票抬头:'+top_str+' 发票内容:'+content_str;
    document.getElementById('invoice_no').value = invoice_no.value; 
    document.getElementById('invoice_top').value = invoice_top.value; 
    document.getElementById('invoice_content').value = invoice_content.value;
    TB_remove();
      
}


  
function subForm()
{
	
	if(document.getElementById("goods_num").value=="")
	{
	  alert("请输入购买数量！");
	  return;
	}
  if(document.getElementById("shop_price").value=="")
	{
	  alert("请输入商品价格！");
	  return;
	}
  if(document.getElementById("shipping_fee").value=="")
	{
	  alert("请输入配送费用！");
	  return;
	}
 
  if(document.getElementById("pack_fee").value=="")
	{
	  alert("请输入包装费用！");
	  return;
	}
  
  if(trim(document.getElementById("tax_invoice").value)=="")
	{
	  alert("请输入发票税额！");
	  return;
	}
  
  if(trim(document.getElementById("paid_amount").value)=="")
	{
	  alert("请输入已付款金额！");
	  return;
	}
  
  document.addForm.submit();	

}


function selectOne(val)
{
	var checkall = document.getElementById('checkone_'+val);
	var listsize = document.getElementById('checksize_'+val).value;
	for(var i=0;i<listsize;i++)
	{
			if(checkall.checked)
			{
				document.getElementById('check_'+val+'_'+i).checked = true;
			}
			else
			{
				document.getElementById('check_'+val+'_'+i).checked = false;
			}
	}
}

function checkedF(val1,val2)
{
   if(document.getElementById('check_'+val1+'_'+val2).checked)
   {
      document.getElementById('checkone_'+val1).checked = true; 
   }
   else
   {
        var listsize = document.getElementById('checksize_'+val1).value;
				var cflag =0;
				for(var i=0;i<listsize;i++)
				{
						if(document.getElementById('check_'+val1+'_'+i).checked)
						{
						 cflag  =1;
						 break;
						}
				 }
       if(cflag ==1)
       {
          document.getElementById('checkone_'+val1).checked = true; 
       }
       else
       {
          document.getElementById('checkone_'+val1).checked = false; 
       }   
   }     

}
  

function setNextAction()
{
  var ifsub = document.getElementById('ifsub1').value;
  
  var s_next_desc = document.getElementById('s_next_desc').value 
  
  var s_next_date = document.getElementById('s_next_date').value; 
  if(document.getElementById('ifsub1').checked)
  {
    
    if(trim(s_next_date)=='')
    {
       alert('请输入预约时间！'); 
       return;   
    }
    if(trim(s_next_desc)=='')
    {
       alert('请输入预约操作说明！'); 
       return;   
    }
    document.getElementById('subaction').value ='1';  
  }  
    
  document.getElementById('action_desc').value = document.getElementById('s_action_desc').value;
  document.getElementById('next_date').value = s_next_date;
  var next_desc = s_next_desc; 
  if(next_desc.length>480)
  {
      next_desc =next_desc.substring(0,480); 
  }  
  document.getElementById('next_desc').value = next_desc;
  
  document.indexForm.action='/doTradeReg.do';
  
  document.indexForm.submit();
  
  TB_remove();

}  
 
 function getOneOrderState(val)
 {
   
   //alert(val);   
    //--1 
    if(val.indexOf('a')>-1)
    {
      return '0';
    }
    
    var or_state = val.split('|');    
    
    //--2
    var var_b=''; 
    for(var i=0;i<or_state.length;i++)
    {
         var o_s_b = or_state[i];	       
	       if(o_s_b.indexOf('b')>-1)
	       {
	         var_b +='0';       
	       }
	       else if(o_s_b.indexOf('c')>-1 || o_s_b.indexOf('d')>-1)	       
	       {
           var_b +='1';           	       
	       }
	       else
	       {
           var_b +='2'; 	       
	       }    
    }    
    
    if((var_b.indexOf('2')==-1) && (var_b.indexOf('0')>-1))
    {
       return '1';    
    }
   
    //--3
    var var_c=''; 
    for(var i=0;i<or_state.length;i++)
    {
	       if(or_state[i].indexOf('c')>-1)
	       {
	         var_c +='0';       
	       }
	       else
	       {
           var_c +='1'; 	       
	       }    
    }    
    
    if((var_c.indexOf('1')==-1) && (var_c.indexOf('0')>-1))
    {
       return '2';    
    }
    
    //--4    
    var var_d=''; 
    
    for(var i=0;i<or_state.length;i++)
    {
         var o_s_d = or_state[i];	       
	       if(o_s_d.indexOf('d')>-1)
	       {
	         var_d +='0';       
	       }
         else if(o_s_d.indexOf('c')>-1)
         {
           var_d +='1';          
         }	       
	       else
	       {
           var_d +='2'; 	       
	       }    
    }    
    
    if((var_d.indexOf('2')==-1) && (var_d.indexOf('0')>-1))
    {
       return '3';    
    }
    
     //--5    
    var var_m=''; 
    for(var i=0;i<or_state.length;i++)
    {
         var o_s_m = or_state[i];	       
	       if(o_s_m.indexOf('m')>-1)
	       {
	         var_m +='0';       
	       }
	       else if(o_s_m.indexOf('c')>-1 || o_s_m.indexOf('d')>-1)	       
	       {
           var_m +='1';           	       
	       }
	       else
	       {
           var_m +='2'; 	       
	       }    
    }    
    
    if((var_m.indexOf('2')==-1) && (var_m.indexOf('0')>-1))
    {
       return '5';    
    }
       	  
 	  return  '4';	
} 

 function getStateName(val)
 {
    if(val=='0'){return '未确认';}
    else if(val=='1'){return '已确认';}
    else if(val=='2'){return '取消';}
    else if(val=='3'){return '无效';}
    else if(val=='4'){return '交易中';} 
    else if(val=='5'){return '交易完成';} 
 } 
 
 function orderOpr(val)
 {
	  var opr = document.getElementById('opr_'+val);
	  if(opr.value=='')
	  {
	     alert('请选择操作方式！'); 
	     return; 
	  }
	  
	  var ifcheck = 0;  
	 	var listsize = document.getElementById('listsize').value;
		for(var i=0;i<listsize;i++)
		{
				if(document.getElementById('checkone_'+i).checked)
	      {
	            ifcheck =1;
	            break;        
	      }      
	  }
    if(ifcheck==0)
    {
       alert('请至少选择一条！'); 
	     return;   
    }
   
   var bpm_id='';
   var order_id_state='';
   var trade_id_state='';
   var trade_id_oprstate=''; 
   var order_id_oprstate='';
   var order_old_state ='';     
   if(opr.value=='b')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
                       //alert(one_state.indexOf('a')==-1 );					           	 
					           	 if(one_state.indexOf('a') > -1 || one_state.indexOf('d') > -1 )
				               {
                           one_state =	one_state.replace('a','');
                           one_state =	one_state.replace('d','');	
                           one_state ='b'+one_state;			               
                           trade_id_state += one_check.value+'|'+one_state+',';	
                           trade_id_oprstate += one_check.value+'|b|已确认,';                           				              
				               }
                       else
                       {
                           alert('此操作只能对未确认或无效的订单操作，请重新选择！');
                           return;                       
                       } 
                        			               				                                      				                
				            }
				            all_state +=	one_state+'|';      
					      
					       } 
                
                 var o_state = getOneOrderState(all_state);             
                 order_id_state  += checkone.value+'|'+o_state+',';	
                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
                 
                 var old_state =  document.getElementById('order_old_state_'+i).value;               
                 order_old_state += checkone.value+'|'+old_state+',';         
	          
	          }	    
	   
	     }
 
      bpm_id ='5858';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;   
   }  
   
   if(opr.value=='d')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('c')>-1)
				               {
                             alert('此操作不能对取消的订单操作，请重新选择！');
                             return;				              
				               }
                        
                        one_state =	one_state.replace('a','');
                        one_state =	one_state.replace('b','');	
                        one_state ='d'+one_state;			               
                        trade_id_state += one_check.value+'|'+one_state+',';
                        trade_id_oprstate += one_check.value+'|d|无效,'; 				               				                                      				                
				            }
				            all_state +=	one_state+'|';      
					      
					       }
                 var o_state = getOneOrderState(all_state);             
                 order_id_state  += checkone.value+'|'+o_state+',';	
                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
                 var old_state =  document.getElementById('order_old_state_'+i).value;               
                 order_old_state += checkone.value+'|'+old_state+',';         
	          
	          
	          }	    
	   
	     }
 
      bpm_id ='5859';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;           
   } 
   
   if(opr.value=='f')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('b')==-1)
				               {
                             alert('此操作只针对已经确认的订单进行操作！');
                             return;				              
				               }
				               if(one_state.indexOf('e')==-1)
				               {
                             alert('此操作只针对未发货的订单进行操作！');
                             return;				              
				               }
				                                       
                        one_state =	one_state.replace('e','');
                        one_state ='f'+one_state;			               
                        trade_id_state += one_check.value+'|'+one_state+',';
                        trade_id_oprstate += one_check.value+'|f|配货中,'; 				               				                                      				                
				            }
				            all_state +=	one_state+'|';      
					      
					       }
                 var o_state = getOneOrderState(all_state);             
                 order_id_state  += checkone.value+'|'+o_state+',';	
                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
                 var old_state =  document.getElementById('order_old_state_'+i).value;               
                 order_old_state += checkone.value+'|'+old_state+',';           
	          
	          }	    
	   
	     }
 
      bpm_id ='5860';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;                
   }   
  
  if(opr.value=='g')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('b')==-1)
				               {
                             alert('所选中含有尚未确认的订单，请先确认订单！');
                             return;				              
				               }
				               if(one_state.indexOf('g')>-1)
				               {
                             alert('所选中含有已经发货的订单，请重新选择！');
                             return;				              
				               }
				                                       
                        one_state =	one_state.replace('e','');
                        one_state =	one_state.replace('f','');
                        one_state ='g'+one_state;			               
                        trade_id_state += one_check.value+'|'+one_state+',';
                        trade_id_oprstate += one_check.value+'|g|已发货,';				               				                                      				                
				            }
				            all_state +=	one_state+'|';      
					      
					       }
                 var o_state = getOneOrderState(all_state);             
                 order_id_state  += checkone.value+'|'+o_state+',';	
                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
                 var old_state =  document.getElementById('order_old_state_'+i).value;               
                 order_old_state += checkone.value+'|'+old_state+',';              
	          
	          }	    
	   
	     }
 
      bpm_id ='5861';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;                 
   }

   
   if(opr.value=='j')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('j')>-1)
				               {
                             alert('所选中含有已经付款的订单，请重新选择！');
                             return;				              
				               }
				                                       
                        one_state =	one_state.replace('i','');
                        one_state ='j'+one_state;			               
                        trade_id_state += one_check.value+'|'+one_state+',';
                        trade_id_oprstate += one_check.value+'|j|已付款,';				               				                                      				                
				            }
				             all_state +=	one_state+'|';        
					      
					       }
                     var o_state = getOneOrderState(all_state);             
		                 order_id_state  += checkone.value+'|'+o_state+',';	
		                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
		                 var old_state =  document.getElementById('order_old_state_'+i).value;               
		                 order_old_state += checkone.value+'|'+old_state+',';                 
	          
	          }	    
	   
	     }
 
      bpm_id ='5862';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;        
         
   }

   
  if(opr.value=='l')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('k')==-1)
				               {
                             alert('该操作只对会员申请退款的订单进行操作！');
                             return;				              
				               }
				                                       
                        one_state =	one_state.replace('k','');
                        one_state ='l'+one_state;			               
                        trade_id_state += one_check.value+'|'+one_state+',';
                        trade_id_oprstate += one_check.value+'|l|退款确认,';					               				                                      				                
				            }
				            all_state +=	one_state+'|';      
					      
					       }
                    var o_state = getOneOrderState(all_state);             
		                 order_id_state  += checkone.value+'|'+o_state+',';	
		                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
		                 var old_state =  document.getElementById('order_old_state_'+i).value;               
		                 order_old_state += checkone.value+'|'+old_state+',';                      
	          
	          }	    
	   
	     }
 
      bpm_id ='5863';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;              
   }
     
  if(opr.value=='m')
   {
      for(var i=0;i<listsize;i++)
		  {
					var checkone = document.getElementById('checkone_'+i);				
					var all_state='';				
 
          if(checkone.checked)
					{
								var onesize = document.getElementById('checksize_'+i).value;
					      for(var j=0;j<onesize;j++)
					      {
					         
                   var one_state = document.getElementById('state_'+i+'_'+j).value; 
                   var one_check = document.getElementById('check_'+i+'_'+j);		         
					         if(one_check.checked)
					         {
					           	 if(one_state.indexOf('b')==-1)
				               {
                             alert('所选中含有尚未确认的订单！');
                             return;				              
				               }
				               if(one_state.indexOf('h')>-1 || one_state.indexOf('l')>-1)
				               {
                           one_state ='m'+one_state;
                           one_state = one_state.replace('b','');
                           trade_id_state += one_check.value+'|'+one_state+',';	
                           trade_id_oprstate += one_check.value+'|m|交易完成,';		  		              
				               }
				               else
				               {
                           alert('请选择收货确认或退款确认的订单进行操作！');
                           return;						               
				               }				                                       
				            }
				            all_state +=	one_state+'|';      
					      
					       }
                     var o_state = getOneOrderState(all_state);             
		                 order_id_state  += checkone.value+'|'+o_state+',';	
		                 order_id_oprstate += checkone.value+'|'+o_state+'|'+getStateName(o_state)+',';	
		                 var old_state =  document.getElementById('order_old_state_'+i).value;               
		                 order_old_state += checkone.value+'|'+old_state+',';             	          
	          }	    
	   
	     }
 
      bpm_id ='5864';  
      document.getElementById('order_id_state').value = order_id_state; 
      document.getElementById('trade_id_state').value = trade_id_state; 
      document.getElementById('trade_id_oprstate').value = trade_id_oprstate; 
      document.getElementById('order_id_oprstate').value = order_id_oprstate; 
      document.getElementById('order_old_state').value = order_old_state;              
   }
 
   document.getElementById('bpm_id').value = bpm_id;       
   
   var rdata = Math.round(Math.random()* 10000);
 
   var gURL = "/program/admin/myorder/orderAction.jsp?height=200&width=500&rdata="+rdata;  
       
   TB_show('订单操作说明',gURL,false);	 

 }